﻿//必须要引用的文件
using System;
using System.IO;
using System.Windows.Forms;
using log4net;
using log4net.Config;
using log4net.Repository;

//[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = false)]
namespace YJYpublicClass
{
    public class myLog4
    {
        static ILog loger = null;
        static RichTextBox logdisrichtext = null;
        public myLog4() {
        }
        //日志类初始化（读取配置文件）
        public static void LogInit()
        {

            loger = LogManager.GetLogger(typeof(myLog4));
            loger.Info("系统初始化Logger模块");
        }
        public static void LogInit(RichTextBox logdisp)
        {
            loger = LogManager.GetLogger(typeof(myLog4));
            loger.Info("系统初始化Logger模块");
            logdisrichtext = logdisp;
        }

        /// <summary>
        /// Log4日志
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="ex"></param>
        public static void Error(string msg = "出现异常", Exception ex = null)
        {
           // Console.WriteLine(msg);
            loger.Error(msg, ex);
        }

        /// <summary>
        /// Log4日志
        /// </summary>
        /// <param name="msg"></param>
        public static void Warn(string msg)
        {
           // Console.WriteLine(msg);
            loger.Warn(msg);
        }

        /// <summary>
        /// Log4日志
        /// </summary>
        /// <param name="msg"></param>
        public static void Info(string msg)
        {
            // Console.WriteLine(msg);
            string LogTime = System.DateTime.Now.ToString("HH:mm:ss");
            string   LogMessage = LogTime + ":" + msg + "\r\n";      
            loger.Info(LogMessage);
            if(logdisrichtext!=null)
            {
                logdisrichtext.Invoke((EventHandler)delegate {

                    if (logdisrichtext.Lines.Length > 30)
                    {
                        logdisrichtext.Clear();
                    }
                    logdisrichtext.AppendText(LogMessage);
                    //保持在最后一行显示
                    logdisrichtext.ScrollToCaret();

                });
            }
        }

        /// <summary>
        /// Log4日志
        /// </summary>
        /// <param name="msg"></param>
        public static void Debug(string msg)
        {
          //  Console.WriteLine(msg);
            loger.Debug(msg);
        }


    }
}
